<?php

class CodeIslands_Runner
{
	private $_template_engine = null;
	
	public function __construct($template_engine)
	{
		$this->_template_engine = $template_engine;
	}
	
	public function fromFile($filename)
	{
		return $this->_template_engine->fetch(null, $filename);	
	}
	
	public function fromDirectoryRecursive($path, $filename)
	{
		$dir_iterator = new RecursiveDirectoryIterator($path);
		$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
		
		foreach ($iterator as $file) {
			$isValid = substr_compare($file, $filename, -strlen($filename), strlen($filename)) === 0;
			if($isValid)
			{
				$output = $this->fromFile($file);
				$fp = fopen($file . '.php', 'w');
				fwrite($fp, $output);
				fclose($fp);
			}
		}
	}
}